package com.ht.yml.tools;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.aspectj.util.FileUtil;
import org.yaml.snakeyaml.Yaml;

import java.io.File;
import java.util.Map;

public class YamlReader {
    public static void main(String[] args) {
        String fileName1 = "D:\\恒太git代码\\1java-base\\yml-tools\\application.yml";
        String fileName2 = "D:\\恒太git代码\\1java-base\\yml-tools\\application-pro.yml";

        //Map<String, Object> yamlMap = readFile(fileName1);
        //System.out.println(JSON.toJSONString(yamlMap, SerializerFeature.PrettyFormat));

        Map<String, Object> yamlMap = readFiles(fileName1, fileName2);
        System.out.println(JSON.toJSONString(yamlMap, SerializerFeature.PrettyFormat));
    }

    // 读取一个yml文件
    public static Map<String, Object> readFile(String fileName) {
        try {
            String content = FileUtil.readAsString(new File(fileName));
            return parseContent(content);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    // 读取 fileName1 和 fileName2 : 相同的属性, fileName2 会覆盖 fileName1
    public static Map<String, Object> readFiles(String fileName1, String fileName2) {
        Map<String, Object> c1 = readFile(fileName1);
        Map<String, Object> c2 = readFile(fileName2);

        String n1 = (new File(fileName1)).getName();
        String n2 = (new File(fileName2)).getName();
        YamlPropertiesUtils.mergeProperties(c1, n1, c2, n2, false, true);
        return c1;
    }

    public static Map<String, Object> parseContent(String content) {
        Yaml yaml = new Yaml();
        try {
            return yaml.load(content);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    public static String dump(Map<String, Object> map) {
        StringBuilder sb = new StringBuilder();
        dump("", map, sb);
        return sb.toString();
    }
    private static void dump(String stageStr, Map<String, Object> map, StringBuilder sb) {
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            final String key = entry.getKey();
            final Object value = entry.getValue();

            if (value instanceof Map) {
                if (!((Map) value).isEmpty()) {
                    StringBuilder subSB = new StringBuilder();
                    dump(stageStr + "  ", (Map) value, subSB);

                    if (subSB.length() > 0) {
                        sb.append(stageStr).append(key).append(":\n");
                        sb.append(subSB.toString());
                    } else {
                        continue;
                    }
                } else {
                    continue;
                }
            } else {
                sb.append(stageStr).append(key).append(": ");
                if (value != null) sb.append(value);
                sb.append('\n');
            }

            if (stageStr.isEmpty()) {
                sb.append("\n\n");
            }
        }
    }
}
